package com.pyue.eam.admin.utils;

import com.pyue.eam.common.core.util.SpringContextHolder;

import java.util.concurrent.Future;

/**
 * @author tanniao 2021-2-24 10:37:05
 */
public class AsyncUtil {

    private static AsyncExecute asyncExecute;
    private static final Object LOCK = new Object();

    private static synchronized void init() {
        if (asyncExecute == null) {
            synchronized (LOCK) {
                try {
                    asyncExecute = SpringContextHolder.getApplicationContext().getBean(AsyncExecute.class);
                } catch (Exception e) {
                    asyncExecute = new AsyncExecute();
                }
            }
        }
    }

    public static void execute(Runnable run) {
        init();
        asyncExecute.execute(run);
    }

    public static void executeAtHttp(Runnable run) {
        init();
        asyncExecute.executeAtHttp(run);
    }

    public static Future<?> submit(Runnable run) {
        init();
        return asyncExecute.submit(run);
    }

    public static Future<?> submitAtHttp(Runnable run) {
        init();
        return asyncExecute.submitAtHttp(run);
    }

}
